package com.winshare.saleAssistant.service.impl;

import com.winshare.saleAssistant.dao.StatisticsMapper;
import com.winshare.saleAssistant.domain.dto.req.*;
import com.winshare.saleAssistant.domain.dto.resp.*;
import com.winshare.saleAssistant.domain.entity.StaffDO;
import com.winshare.saleAssistant.domain.entity.StatisticsDO;
import com.winshare.saleAssistant.domain.query.StatisticReq3Query;
import com.winshare.saleAssistant.domain.query.StatisticReq4Query;
import com.winshare.saleAssistant.domain.query.StatisticReq5Query;
import com.winshare.saleAssistant.domain.query.StatisticReq6Query;
import com.winshare.saleAssistant.mapper.StatisticsMapStruct;
import com.winshare.saleAssistant.service.OrganizationService;
import com.winshare.saleAssistant.service.StatisticsService;
import com.winshare.saleAssistant.util.Page;
import com.winshare.saleAssistant.util.RequestHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class StatisticsServiceImpl implements StatisticsService {

    private final StatisticsMapper statisticsMapper;

    private final OrganizationService organizationService;

    public StatisticsServiceImpl(StatisticsMapper statisticsMapper, OrganizationService organizationService) {
        this.statisticsMapper = statisticsMapper;
        this.organizationService = organizationService;
    }

    @Override
    public StatisticResp1DTO getActiveStaffAndTotalStaffAmount(StatisticReq1DTO statisticReq1DTO) {
        StatisticResp1DTO statisticResp1DTO = statisticsMapper.getActiveStaffAndTotalStaffAmount(statisticReq1DTO);
        return statisticResp1DTO;
    }

    @Override
    public StatisticResp2DTO getNum2(StatisticReq1DTO statisticReq1DTO) {
        StatisticResp2DTO statisticResp2DTO = statisticsMapper
                .getArticleAndProductReadAndPromotionAndArticleSubmitCount(statisticReq1DTO);
        return statisticResp2DTO;
    }

    @Override
    public Page<StatisticResp3DTO> getOrgUsage(StatisticReq3DTO statisticReq3DTO) {
        StatisticReq3Query statisticReq3Query = StatisticsMapStruct.INSTANCE.fromStatisticReq3DTO(statisticReq3DTO);
        statisticReq3Query.setOffsetIndex(statisticReq3DTO.getOffsetIndex());
        List<Integer> childrenOrgIdList = new ArrayList<>();
        if (null != statisticReq3Query.getOrgId()) {
            childrenOrgIdList = organizationService.getChildrenOrgList(statisticReq3Query.getOrgId(), statisticReq3DTO.getOrgType());
        }
        if (CollectionUtils.isEmpty(childrenOrgIdList)) {
            statisticReq3Query.setOrgIdList(childrenOrgIdList);
        }
        int totalCount = statisticsMapper.getOrgUsageCount(statisticReq3Query);
        Integer pageIndex = statisticReq3DTO.getPageIndex();
        Integer pageSize = statisticReq3DTO.getPageSize();
        if (0 == totalCount) {
            return new Page<>(pageIndex, pageSize, totalCount, new ArrayList<>());
        }
        List<StatisticResp3DTO> statisticResp3DTOList = statisticsMapper.getOrgUsage(statisticReq3Query);
        return new Page<>(pageIndex, pageSize, totalCount, statisticResp3DTOList);
    }

    @Override
    public Page<StatisticResp4DTO> getProductFocusInfo(StatisticReq4DTO statisticReq4DTO) {
        StatisticReq4Query statisticReq4Query = StatisticsMapStruct.INSTANCE.fromStatisticReq4DTO(statisticReq4DTO);
        statisticReq4Query.setOffsetIndex(statisticReq4DTO.getOffsetIndex());
        int totalCount = statisticsMapper.getProductFocusInfoCount(statisticReq4Query);
        Integer pageIndex = statisticReq4DTO.getPageIndex();
        Integer pageSize = statisticReq4DTO.getPageSize();
        if (0 == totalCount) {
            return new Page<>(pageIndex, pageSize, totalCount, new ArrayList<>());
        }
        List<StatisticResp4DTO> statisticResp4DTOList = statisticsMapper.getProductFocusInfo(statisticReq4Query);
        return new Page<>(pageIndex, pageSize, totalCount, statisticResp4DTOList);
    }

    @Override
    public Page<StatisticResp5DTO> getArticleReadOrder(StatisticReq5DTO statisticReq5DTO) {
        StatisticReq5Query statisticReq5Query = StatisticsMapStruct.INSTANCE.fromStatisticReq5DTO(statisticReq5DTO);
        statisticReq5Query.setOffsetIndex(statisticReq5DTO.getOffsetIndex());
        int totalCount = statisticsMapper.getArticleReadOrderCount(statisticReq5Query);
        Integer pageIndex = statisticReq5DTO.getPageIndex();
        Integer pageSize = statisticReq5DTO.getPageSize();
        if (0 == totalCount) {
            return new Page<>(pageIndex, pageSize, totalCount, new ArrayList<>());
        }
        List<StatisticResp5DTO> statisticResp5DTOList = statisticsMapper.getArticleReadOrder(statisticReq5Query);
        return new Page<>(pageIndex, pageSize, totalCount, statisticResp5DTOList);
    }

    @Override
    public Page<StatisticResp6DTO> getStaffUsageOrder(StatisticReq6DTO statisticReq6DTO) {
        StatisticReq6Query statisticReq6Query = StatisticsMapStruct.INSTANCE.fromStatisticReq6DTO(statisticReq6DTO);
        statisticReq6Query.setOffsetIndex(statisticReq6DTO.getOffsetIndex());
        int totalCount = statisticsMapper.getStaffUsageOrderCount(statisticReq6Query);
        Integer pageIndex = statisticReq6DTO.getPageIndex();
        Integer pageSize = statisticReq6DTO.getPageSize();
        if (0 == totalCount) {
            return new Page<>(pageIndex, pageSize, totalCount, new ArrayList<>());
        }
        List<StatisticResp6DTO> statisticResp6DTOList = statisticsMapper.getStaffUsageOrder(statisticReq6Query);
        return new Page<>(pageIndex, pageSize, totalCount, statisticResp6DTOList);
    }

    @Override
    public void saveStatistic(StatisticsDO statisticsDO) {
        StaffDO staffDO = RequestHolder.getCurrentUser();
        statisticsDO.setStaffId(staffDO.getId());
        statisticsDO.setStaffName(staffDO.getStaffName());
        statisticsDO.setOrgId(staffDO.getOrgId());
        statisticsDO.setOrgName(staffDO.getOrgName());
        statisticsDO.setCreateDate(LocalDateTime.now().toString());

        StatisticsDO data = statisticsMapper.getStaffData(statisticsDO);
        if (data != null) {
            statisticsDO.setId(data.getId());
            statisticsMapper.updateStaffData(statisticsDO);
        } else {
            statisticsMapper.saveStatistics(statisticsDO);
        }
    }
}
